//
//  MDHButton.swift
//  MMWPT
//
//  Created by 高得华 on 2018/11/26.
//  Copyright © 2018年 高得华. All rights reserved.
//

import UIKit

///设置Button
open class MDHButton: UIButton {
    //MARK: - === 属性
    ///按钮中图片的位置
    @objc public var imageAlignment = MDHButtonImageAlignment.left
    ///按钮中图片与文字的间距
    @objc public var spaceBetweenTitleAndImage: CGFloat = 0
    ///是否使用 高亮状态
    @objc public var highlightState: Bool = false
    ///点击响应区域
    @objc public var hitSize: CGSize = CGSize(width: 0, height: 0)
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        //高亮状态
        self.setUpHighlightState()
    }
    override open func draw(_ rect: CGRect) {
        super.draw(rect)
        //高亮状态
        self.setUpHighlightState()
    }
    
    ///设置高亮状态的方法
    @objc func setUpHighlightState() -> () {
        self.rac_signal(for: UIControl.Event.allTouchEvents).deliverOnMainThread().subscribeNext({ [weak self] (x) in
            if let weakSelf = self {
                weakSelf.isHighlighted = weakSelf.highlightState
            }
        })
    }
    
    ///点击区域
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        super.point(inside: point, with: event)
        let bounds = self.bounds.insetBy(dx: -hitSize.width, dy: -hitSize.height)
        return bounds.contains(point)
    }

    ///添加视图
    override open func layoutSubviews() {
        super.layoutSubviews()
        ///间距
        let space = self.spaceBetweenTitleAndImage
        
        ///titleLabel的宽度
        var titleW = self.titleLabel?.bounds.size.width ?? 0
        ///titleLabel的高度
        let titleH = self.titleLabel?.bounds.size.height ?? 0
        
        ///imageView的宽度
        let imageW = self.imageView?.bounds.size.width ?? 0
        ///imageView的高度
        let imageH = self.imageView?.bounds.size.height ?? 0
        
        ///按钮中心点X的坐标（以按钮左上角为原点的坐标系）
        let btnCenterX = self.bounds.size.width/2
        ///imageView中心点X的坐标（以按钮左上角为原点的坐标系）
        let imageCenterX = btnCenterX - titleW/2
        ///titleLabel中心点X的坐标（以按钮左上角为原点的坐标系）
        let titleCenterX = btnCenterX + imageW/2
        
        ///文本大小
        let textSize = ((self.titleLabel?.text ?? "") as NSString).size(withAttributes: [NSAttributedString.Key.font: (self.titleLabel?.font ?? UIFont.systemFont(ofSize: 13))])
        ///frameSize
        let frameSize = CGSize.init(width: CGFloat(ceilf(Float(textSize.width))), height: CGFloat(ceilf(Float(textSize.height))))
        if (titleW+0.5) < frameSize.width {
            titleW = frameSize.width
        }
        
        
        ///根据类型进行设置图标 和 文字位置
        switch self.imageAlignment {
        case .top:///-(titleCenterX-btnCenterX)  titleCenterX-btnCenterX
            ///使图片和文字水平居中显示
            self.contentHorizontalAlignment = .center
            //let totalHeight = imageH+titleH+space
            self.titleEdgeInsets = UIEdgeInsets.init(top: imageH+space, left: -imageW, bottom: 0, right: 0)
            let bw = self.bounds.width
            self.imageEdgeInsets = UIEdgeInsets.init(top: -(titleH/2 + space/2), left: (bw-imageW)/2.0, bottom: titleH/2 + space/2, right: (bw-imageW)/2.0)//(bw-imageW) -(bw-imageW)
            break
        case .left:
            self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: space/2, bottom: 0, right: -space/2)
            self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -space/2, bottom: 0,right: space)
            break
        case .bottom:
            self.titleEdgeInsets = UIEdgeInsets.init(top: -(imageH/2 + space/2), left: -(titleCenterX-btnCenterX), bottom: (imageH/2 + space/2), right: titleCenterX-btnCenterX)
            self.imageEdgeInsets = UIEdgeInsets.init(top: (titleH/2 + space/2), left: btnCenterX-imageCenterX, bottom: -(titleH/2 + space/2),right: -(btnCenterX-imageCenterX))
            break
        case .right:
            self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -(imageW + space/2), bottom: 0, right: imageW + space/2)
            self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: titleW+space/2, bottom: 0, right: -(titleW+space/2))
            break
        }
    }
}
